package nju.jun.reader.tieba.crawler;

import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import nju.jun.reader.tieba.model.Tieba;
import nju.jun.reader.tieba.model.TiebaNovel;

public class TiebaCrawler implements Runnable {

	private Tieba tieba;
	
	public TiebaCrawler() {
		tieba = Tieba.instance();
	}
	
	private static final int SLEEP_INTERVAL = 15 * 60 * 1000;
	
	public void run() {
		while (true) {
			crawl();
			try {
				Thread.sleep(SLEEP_INTERVAL);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static final int MAX_THREAD = 5;
	
	private static final int TIME_OUT = 10; // 10 minutes
	
	public void crawl() {
		if (tieba.getNovels().isEmpty()) {
			return;
		}
		ExecutorService es = Executors.newFixedThreadPool(MAX_THREAD);
		Iterator<TiebaNovel> iter = tieba.getNovels().iterator();
		while (iter.hasNext()) {
			es.execute(new TiebaNovelCrawler(iter.next()));
		}
		es.shutdown();
		try {
			es.awaitTermination(TIME_OUT, TimeUnit.MINUTES);
		} catch (InterruptedException e) {
			if (!es.isShutdown()) {
				es.shutdownNow();
			}
		}
	}
	
}
